package com.papers.juan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.papers.common.utils.StringUtils;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.papers.juan.domain.bo.SysFileBo;
import com.papers.juan.domain.vo.SysFileVo;
import com.papers.juan.domain.SysFile;
import com.papers.juan.mapper.SysFileMapper;
import com.papers.juan.service.ISysFileService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文件Service业务层处理
 *
 * @author papers
 * @date 2022-11-08
 */
@RequiredArgsConstructor
@Service
public class SysFileServiceImpl implements ISysFileService {

    private final SysFileMapper baseMapper;

    /**
     * 查询文件
     */
    @Override
    public SysFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件列表
     */
    @Override
    public TableDataInfo<SysFileVo> queryPageList(SysFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysFile> lqw = buildQueryWrapper(bo);
        Page<SysFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件列表
     */
    @Override
    public List<SysFileVo> queryList(SysFileBo bo) {
        LambdaQueryWrapper<SysFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysFile> buildQueryWrapper(SysFileBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysFile::getUrl, bo.getUrl());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), SysFile::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getFileSize()), SysFile::getFileSize, bo.getFileSize());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPublic()), SysFile::getIsPublic, bo.getIsPublic());
        lqw.eq(StringUtils.isNotBlank(bo.getFileSurfix()), SysFile::getFileSurfix, bo.getFileSurfix());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDel()), SysFile::getIsDel, bo.getIsDel());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysFile::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getClassify()), SysFile::getClassify, bo.getClassify());
        lqw.eq(bo.getDataId() != null, SysFile::getDataId, bo.getDataId());
        lqw.eq(bo.getFileSort() != null, SysFile::getFileSort, bo.getFileSort());
        lqw.eq(bo.getCreateUser() != null, SysFile::getCreateUser, bo.getCreateUser());
        lqw.eq(bo.getUpdateUser() != null, SysFile::getUpdateUser, bo.getUpdateUser());
        return lqw;
    }

    /**
     * 新增文件
     */
    @Override
    public Boolean insertByBo(SysFileBo bo) {
        SysFile add = BeanUtil.toBean(bo, SysFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件
     */
    @Override
    public Boolean updateByBo(SysFileBo bo) {
        SysFile update = BeanUtil.toBean(bo, SysFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
